package com.houtai.controller.activity.works;

import com.aliyun.openservices.oss.model.ObjectMetadata;
import com.houtai.authority.Authority;
import com.houtai.authority.AuthorityType;
import com.houtai.base.ResultVo;
import com.houtai.bean.dto.avtivity.works.TdActivityWorksDetailDto;
import com.houtai.bean.dto.avtivity.works.TdActivityWorksInsertDto;
import com.houtai.bean.dto.avtivity.works.TdActivityWorksListDto;
import com.houtai.bean.dto.avtivity.works.comments.TdActivityWorksCommentsInsertDto;
import com.houtai.bean.dto.avtivity.works.comments.TdActivityWorksCommentsListDto;
import com.houtai.bean.dto.praise.PraiseDto;
import com.houtai.bean.vo.activity.works.TdActivityWorksDetailVo;
import com.houtai.bean.vo.activity.works.TdActivityWorksListVo;
import com.houtai.bean.vo.activity.works.comments.TdActivityWorksCommentsListVo;
import com.houtai.bean.vo.dict.DictListResult;
import com.houtai.bean.vo.file.FileVo;
import com.houtai.common.Contant;
import com.houtai.common.oss.OssUtil;
import com.houtai.common.util.CommonUtil;
import com.houtai.service.activity.works.ActivityWorksService;
import com.houtai.service.dict.DictService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.*;

@RestController
@Api(value="作品接口", tags={"作品接口"})
@RequestMapping(value = {"api/v1"}, method = RequestMethod.POST)
public class ActivityWorksContorller {
    @Autowired
    private DictService dictService;
    @Autowired
    private ActivityWorksService activityWorksService;



    @Authority(value = AuthorityType.NoValidate)
    @ApiOperation(value="作品类别列表", notes="作品类别列表")
    @RequestMapping(value = "/activity/works/type", method = RequestMethod.POST)
    public DictListResult type() {
        return dictService.findDictByType("works_type");
    }

    @Authority(value = AuthorityType.Login)
    @ApiOperation(value="作品上传文件(次接口为表单类型)", notes="作品上传文件")
    @RequestMapping(value = "/activity/works/file", method = RequestMethod.POST)
    public FileVo file(@RequestParam(value = "frontFile", required = true) MultipartFile frontFile)throws Exception{


        FileVo vo = new FileVo();
        if (CommonUtil.checkFileSize(frontFile.getSize(), 10,"M")) {
            SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddhhmmssS", Locale.CHINA);

            String frontFileName = frontFile.getOriginalFilename().substring(frontFile.getOriginalFilename().lastIndexOf(".") + 1);
            String imgName = formatter.format(new Date(System.currentTimeMillis()));
            String frontSavePath = Contant.FILE_IMG + "/activity/works/" + imgName + "." + frontFileName;
            ObjectMetadata frontMetadata = new ObjectMetadata();

            InputStream inputStream = frontFile.getInputStream();
            frontMetadata.setContentLength(inputStream.available());
            OssUtil.uploadInputStream(inputStream, frontMetadata, frontSavePath);
            vo.setMark("0");
            vo.setTip("查询成功");
            vo.setFile_name(imgName + "." + frontFileName);
            vo.setFile_url(Contant.DOMAIN +  Contant.FILE_IMG + "/activity/works/" + imgName + "." + frontFileName);
        } else {
            vo.setMark("1");
            vo.setTip("作品文件最大10M");
        }

        return vo;
    }
    @ApiOperation(value="作品添加", notes="作品添加")
    @Authority(AuthorityType.Login)
    @RequestMapping(value = "/activity/works/insert")
    public ResultVo insert(@ApiParam(value = "作品添加所需参数") @Valid @RequestBody TdActivityWorksInsertDto dto, BindingResult result,
                           @RequestHeader String token) {
        ResultVo retInfo = new ResultVo();
        if (result.hasErrors()) {
            List<FieldError> err = result.getFieldErrors();
            FieldError fe = err.get(0);
            retInfo.setMark("1");
            retInfo.setTip(fe.getDefaultMessage());
        } else {
            retInfo = activityWorksService.insertActivityWorks(dto, token);
        }
        return retInfo;
    }
    @ApiOperation(value="作品列表", notes="作品列表")
    @Authority(AuthorityType.NoValidate)
    @RequestMapping(value = "/activity/works/list")
    public TdActivityWorksListVo insert(@ApiParam(value = "作品列表所需参数") @Valid @RequestBody TdActivityWorksListDto dto, BindingResult result) {
        TdActivityWorksListVo retInfo = new TdActivityWorksListVo();
        if (result.hasErrors()) {
            List<FieldError> err = result.getFieldErrors();
            FieldError fe = err.get(0);
            retInfo.setMark("1");
            retInfo.setTip(fe.getDefaultMessage());
        } else {
            retInfo = activityWorksService.activityWorksList(dto);
        }
        return retInfo;
    }
    @ApiOperation(value="作品评论列表", notes="作品评论列表")
    @Authority(AuthorityType.NoValidate)
    @RequestMapping(value = "/activity/works/comments/list")
    public TdActivityWorksCommentsListVo comments(@ApiParam(value = "活动评论列表所需参数") @Valid @RequestBody TdActivityWorksCommentsListDto dto, BindingResult result) {
        TdActivityWorksCommentsListVo retInfo = new TdActivityWorksCommentsListVo();
        if (result.hasErrors()) {
            List<FieldError> err = result.getFieldErrors();
            FieldError fe = err.get(0);
            retInfo.setMark("1");
            retInfo.setTip(fe.getDefaultMessage());
        } else {
            retInfo = activityWorksService.activityWorksCommentsList(dto);
        }
        return retInfo;
    }
    @ApiOperation(value="作品评论", notes="作品评论")
    @Authority(AuthorityType.Login)
    @RequestMapping(value = "/activity/works/comments")
    public ResultVo apply(@ApiParam(value = "作品评论所需参数") @Valid @RequestBody TdActivityWorksCommentsInsertDto dto, BindingResult result,
                          @RequestHeader String token) {
        ResultVo retInfo = new ResultVo();
        if (result.hasErrors()) {
            List<FieldError> err = result.getFieldErrors();
            FieldError fe = err.get(0);
            retInfo.setMark("1");
            retInfo.setTip(fe.getDefaultMessage());
        } else {
            retInfo = activityWorksService.activityWorksComments(dto, token);
        }
        return retInfo;
    }
    @ApiOperation(value="作品详情", notes="作品详情")
    @Authority(AuthorityType.NoValidate)
    @RequestMapping(value = "/activity/works/detail")
    public TdActivityWorksDetailVo detail(@ApiParam(value = "作品详情所需参数") @Valid @RequestBody TdActivityWorksDetailDto dto, BindingResult result,
                                          @RequestHeader String token) {
        TdActivityWorksDetailVo retInfo = new TdActivityWorksDetailVo();
        if (result.hasErrors()) {
            List<FieldError> err = result.getFieldErrors();
            FieldError fe = err.get(0);
            retInfo.setMark("1");
            retInfo.setTip(fe.getDefaultMessage());
        } else {
            retInfo = activityWorksService.activityWorksDetail(dto, token);
        }
        return retInfo;
    }
    @ApiOperation(value="作品点赞 取消点赞", notes="品点赞 取消点赞")
    @Authority(AuthorityType.Login)
    @RequestMapping(value = "/activity/works/praise")
    public ResultVo apply(@ApiParam(value = "品点赞 取消点赞所需参数") @Valid @RequestBody PraiseDto dto, BindingResult result,
                          @RequestHeader String token) {
        ResultVo retInfo = new ResultVo();
        if (result.hasErrors()) {
            List<FieldError> err = result.getFieldErrors();
            FieldError fe = err.get(0);
            retInfo.setMark("1");
            retInfo.setTip(fe.getDefaultMessage());
        } else {
            retInfo = activityWorksService.praise(dto, token);
        }
        return retInfo;
    }
}
